package es.denisneuf.core.imanagers 
{

	/**
	* @author Daniel Alvaro
	* @work dalvaro@ifoline.com
	* @gmail znpobbx@gmail.com
	* @msn denisneuf@hotmail.com
	* @twitter znpobbx
	**/

	import flash.display.MovieClip;	
	//import es.denisneuf.core.imanagers.Template;
	//import es.denisneuf.core.imanagers.SingleContent;
	import mx.core.ClassFactory;
	import flash.utils.getDefinitionByName;
	import flash.utils.describeType;
	import flash.utils.*;

	//import mx.core.IFactory;
	
	import es.denisneuf.core.LoggerSupport;
	import com.luminicbox.log.Logger;


	public class TemplateManager extends LoggerSupport
	{
		public static var className:String = "TemplateManager";
		public static var packageName:String = "es.denisneuf.core.imanagers";
		public static var version:String = "1.0.00";
		public static var author:String = "Daniel Alvaro";
		
		private var _container:MovieClip;
		private static var instance:TemplateManager = null;
		
		//private var temp:Class; //Created to access instance of class type
		
		private var _log:Logger;
		
		//INFO
		//http://flexiblegorilla.com/wordpress/?p=140
			
		public function TemplateManager():void 
		{		
			
			_log = getLogger();
			
			instantiateUsingClass( es.denisneuf.core.imanagers.SingleContent );
			instantiateUsingClass( es.denisneuf.core.imanagers.CaptivateContent );
			instantiateUsingClass( es.denisneuf.core.imanagers.PracticaContent );
		}
		
		
	    public static function getInstance():TemplateManager 
	    {
	         if (instance == null) {
	            instance = new TemplateManager ();
	          }
	         return instance;
	    }
	    
	    public function setMovieClipContainer (_newcontainer:MovieClip):void
	    {	
	    	_container = _newcontainer;
	    }
	    
	    public static function instantiateUsingClass(classToInstantiate:Class):* 
	    {
			var myClassFactory : ClassFactory = new ClassFactory(classToInstantiate);
			var myObjectInstance : * = myClassFactory.newInstance();
			return myObjectInstance;
		}
	    
	    
		public static function instantiateUsingClassName(className:String):* 
		{
			var classToInstantiate : Class = getDefinitionByName(className) as Class;
			var myClassFactory : ClassFactory = new ClassFactory(classToInstantiate);
			var myObjectInstance : * = myClassFactory.newInstance();
			return myObjectInstance;
		}
		
		public static function getMethods(classOfInterest:Class):Array 
		{
			var xmlDescriptionOfClass:XML = describeType(classOfInterest);
			var nonstaticMethodsXML:XMLList = xmlDescriptionOfClass['factory'].method;
		
			var methods:Array = [];
			for each (var methodXML:XML in nonstaticMethodsXML) {
				methods.push(methodXML.@name);
			}
			return methods;
		}		
			
			
		public static function invokeMethod(objectContainingMethod:*, methodName:String, parms:Array):* 
		{
			var method:Function = objectContainingMethod[methodName];
			var returnValue:* = method.apply(objectContainingMethod, parms);
		
			return returnValue;
		}			    	
	    
		public function getTemplate (xmlTemplate:XML):*
		{
			
			var _data:XML = xmlTemplate;
			var myClassName:* = instantiateUsingClassName( 'es.denisneuf.core.imanagers.' + _data.@template );
			myClassName['xml'] = _data;
			myClassName['container'] = _container;
			var myclass:String = getQualifiedClassName(myClassName);
			var mysuperclass:String = getQualifiedSuperclassName(myClassName);
			
			_log.log('@Class: ' + myclass + " @Superclass: " + mysuperclass + " @container: " + _container);
			_log.log('@myClassName: ' + myClassName);
			//trace ('@Class: ' + myclass + " @Superclass: " + mysuperclass + " @container: " + _container);
			//trace ('invokeMethod.sum ' + invokeMethod(myClassName, 'sum', [2,4]));
			//var xmlDescriptionOfClass:XML = describeType( myClassName );			
			//trace (xmlDescriptionOfClass);
			return myClassName;
		
		}	

	}
}
